    package com.boot.springboot.com.example.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;

    @Configuration
    //让后端允许跨区域，前端调用后端
    public class CorsConfig {
        @Bean
        public CorsFilter corsFilter() {
            CorsConfiguration config = new CorsConfiguration();
            config.addAllowedOriginPattern("*"); // 允许所有域名进行跨域调用
            config.addAllowedHeader("*"); // 允许任何请求头
            config.addAllowedMethod("*"); // 允许任何方法（POST、GET等）
            config.setAllowCredentials(true); // 允许携带凭证

            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", config); // 对所有接口都有效

            return new CorsFilter(source);
        }
    }